package ControllerLayer;
import DomainLayer.*;

public class StockController
{
    private static StockController instance;
    private SuppliersCollection supplierCollection;
    private Items item;
    private Stock stock;
    private Supplier supplier;
    private Request request;
    
    private StockController()
    {
        supplierCollection = SuppliersCollection.getInstance();
        stock = Stock.getInstance();
    }
    
    public static StockController getInstance(){
        if(instance==null){
            instance = new StockController();
        }
        return instance;
    }
    
    public boolean reduceNumberOfProduct(String barCode, int number){
        if(searchItem(barCode).substractNumberOfProduct(number)){
            return true;
        }else{
            return false;
        }
    }
    
    public Items checkStock(){
        item = stock.findItemBelowMinimum();
        return item;
    }
    
    public Supplier searchBestSupplier(){
        supplier = supplierCollection.getSupplier(item.getProduct().getBarCode());
        return supplier;
    }
    
    public void createRequest(int amount){
        if(item.getMaximumAmount()-item.getQuantity()>=amount){
            request = new Request(supplier, amount);
        }
    }
    
    public String printRequest(){
        return request.printRequestInfo();
    }
    
    public void addSupplier(String name, String address, String productTitle, String productCode,double productPrice){
        supplierCollection.addSupplier(new Supplier(name, address, new Product(productTitle,productCode, productPrice)));
    }
    
    public boolean addItem(String productTitle, String barCode, double price, int quantity, int minimumAmount, int maxinimumAmount){
        if(stock.searchForItem(barCode)==null){
            stock.addItem(new Items((new Product(productTitle, barCode, price)), quantity, minimumAmount, maxinimumAmount));
            return true;
        }else{
            return false;
        }
    }
    
    public Items updateItem(int quantity){
        Items updateItem = item;
        updateItem.addNumberOfProduct(quantity);
        return updateItem;
    }
    
    public Items searchItem(String barCode){
        return stock.searchForItem(barCode);
    }
    
    public void clearOrder(){
        request = null;
    }
}